﻿using MediatR;
using T.Materials.API.Application.Command.MaterialStoreCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.API.Application.CommandHandler.MaterialStoreCommandHandler
{
    /// <summary>
    /// 物资与入库删除命令处理器
    /// </summary>
    public class DeletedMaterialStoreCommandHandler : IRequestHandler<DeletedMaterialStoreCommand, int>
    {
        /// <summary>
        /// 物资与入库仓储接口
        /// </summary>
        protected readonly IMaterialStoreRepository materialStoreRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStoreRepository">物资与入库仓储接口</param>
        public DeletedMaterialStoreCommandHandler(IMaterialStoreRepository materialStoreRepository)
        {
            this.materialStoreRepository = materialStoreRepository;
        }

        /// <summary>
        /// 处理删除物资与入库命令
        /// </summary>
        /// <param name="request">删除物资与入库命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Handle(DeletedMaterialStoreCommand request, CancellationToken cancellationToken)
        {
            var res = await materialStoreRepository.GetModelAsync(request.MaterialStoreId);
            res.IsDeleted = true;
            res.DeletedBy = request.DeletedBy;
            res.DeletedDate = DateTime.Now;
            return await materialStoreRepository.UpdateAsync(res);
        }
    }
}
